//
//  AyTableBarController.m
//  weibo
//
//  Created by alvis mac on 8/1/15.
//  Copyright (c) 2015 alvis. All rights reserved.
//

#import "AyTableBarController.h"
#import "AyHomeViewController.h"
#import "AyDiscoverViewController.h"
#import "AyMessageViewController.h"
#import "IWMeViewController.h"
#import "AyHomeViewController.h"
#import "UIImageExtend.h"
#import "AyTabBar.h"
#import "AyDefine.h"
#import "AyNavigationController.h"
#import "AyComoposeViewController.h"
#import "AyUserUnreadCountParame.h"
#import "AyAccountTool.h"
#import "AyUserTool.h"

@interface AyTableBarController()<AyTabBarDelegate>

@property (nonatomic, weak) AyTabBar *customTabBar;
@property (nonatomic, strong)  AyHomeViewController * home;
@property (nonatomic, strong)  AyDiscoverViewController * discover;
@property (nonatomic, strong)  AyMessageViewController * message;
@property (nonatomic, strong)  IWMeViewController * me;
@end



@implementation AyTableBarController


- (void)viewDidLoad{
    [super viewDidLoad];
    [self setupTabbar];
    [self setupAllChiladViewControllers];

    NSTimer *time=[NSTimer scheduledTimerWithTimeInterval:20.0 target:self selector:@selector(checkUnreadCount) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:time forMode:NSRunLoopCommonModes];
}


- (void) checkUnreadCount{
    AyUserUnreadCountParame *param=[AyUserUnreadCountParame param];
    param.uid=@([AyAccountTool account].uid);
    
    [AyUserTool userUnreadCountWithParam:param success:^(AyUserUnreadCountResult *result) {
        // 3.设置badgeValue
        // 3.1.首页
        self.home.tabBarItem.badgeValue = [NSString stringWithFormat:@"%d", result.status];
        
        // 3.2.消息
        self.message.tabBarItem.badgeValue = [NSString stringWithFormat:@"%d", result.messageCount];
        
        // 3.3.我
        self.me.tabBarItem.badgeValue = [NSString stringWithFormat:@"%d", result.follower];
        
        // 4.设置图标右上角的数字
        [UIApplication sharedApplication].applicationIconBadgeNumber = result.count;
    } failure:^(NSError *error) {
        
    }];

}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];

    //delete system uiTabbarbutton
    for (UIView *child in self.tabBar.subviews) {
        if ([child isKindOfClass:[UIControl class]]) {
            [child removeFromSuperview];
        }
    }
}


- (void) setupTabbar{
    AyTabBar *customTabBar = [[AyTabBar alloc] init];
    customTabBar.frame = self.tabBar.bounds;
    customTabBar.delegate = self;
    [self.tabBar addSubview:customTabBar];
    self.customTabBar = customTabBar;
}


- (void)tabBar:(AyTabBar *)tabBar didSelectedButtonFrom:(int)from to:(int)to
{
    self.selectedIndex = to;
    if (to==0) {
        [self.home refresh];
    }
}


- (void) tabBarDidClickedPlusButton:(AyTabBar*) tabBar{
    AyComoposeViewController *compose=[[AyComoposeViewController alloc] init];
    AyNavigationController *nac=[[AyNavigationController alloc] initWithRootViewController:compose];
    [self presentViewController:nac animated:YES completion:nil];
};

- (void) setupAllChiladViewControllers{

    AyHomeViewController *home = [[AyHomeViewController alloc] init];
    [self setupChildViewController:home title:@"首页" imageName:@"tabbar_home" selectedImageName:@"tabbar_home_selected"];
    self.home=home;

    AyMessageViewController *message = [[AyMessageViewController alloc] init];
    [self setupChildViewController:message title:@"消息" imageName:@"tabbar_message_center" selectedImageName:@"tabbar_message_center_selected"];
    self.message=message;

    AyDiscoverViewController *discover = [[AyDiscoverViewController alloc] init];
    [self setupChildViewController:discover title:@"广场" imageName:@"tabbar_discover" selectedImageName:@"tabbar_discover_selected"];
    self.discover=discover;
    
    IWMeViewController *me = [[IWMeViewController alloc] init];
    [self setupChildViewController:me title:@"我" imageName:@"tabbar_profile" selectedImageName:@"tabbar_profile_selected"];
    self.me=me;
}


- (void) setupChildViewController:(UIViewController *)childv title:(NSString *) title
  imageName:(NSString *)imageName  selectedImageName:(NSString *)selectedImageName{
    
    childv.title=title;
    childv.tabBarItem.image=[UIImage imageWithName:imageName];
    UIImage *selectedImage = [UIImage imageWithName:selectedImageName];
    if (iOS7) {
        childv.tabBarItem.selectedImage = [selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    } else {
        childv.tabBarItem.selectedImage = selectedImage;
    }

    AyNavigationController *nav=[[AyNavigationController alloc] initWithRootViewController:childv];
    [self addChildViewController:nav];

    [self.customTabBar addTabBarButtonWithItem:childv.tabBarItem];
}




@end
